package com.mf.morefun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mf.morefun.R;
import com.mf.morefun.listener.ReadDetailTypeListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TestPicSelectAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mIndex;
    private ReadDetailTypeListener listener;

    public ReadDetailTypeListener getListener() {
        return listener;
    }

    public void setListener(ReadDetailTypeListener listener) {
        this.listener = listener;
    }

    public TestPicSelectAdapter(Context mContext, List<String> mIndex) {
        this.mContext = mContext;
        this.mIndex = mIndex;
    }

    @Override
    public int getCount() {
        return mIndex.size();
    }

    @Override
    public Object getItem(int position) {
        return mIndex.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_testpicselect, null);
        } else {
            view = convertView;
        }
        if (null != mIndex && mIndex.size() > 0){
            ViewHolder holder = new ViewHolder(view);
            holder.tvIndex.setText(mIndex.get(position));
            holder.ivPlay.setTag(position);
            holder.ivPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.callBack((int) v.getTag());
                }
            });
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.iv_play)
        ImageView ivPlay;
        @BindView(R.id.tv_index)
        TextView tvIndex;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
